var SmartFreeTable = {
	freeTableLoaded: function(target,id) {
		// var $target = $(target);		
		// $target.removeClass('cke_reset_all');	
		// var $dialogContent = $target.find('.cke_dialog_contents_body');
		// if($dialogContent.length>0){
		// 	$dialogContent.removeAttr('class');
		// 	$dialogContent.addClass('free_table_content');
		// }else{
		//  	$dialogContent = $target.find('.free_table_content');
		// }
		// $dialogContent.children().remove();
		if(id){
			var hotObj = $('body').data(id);
			showHandTable($('#cke_61_uiElement'),hotObj.mergedCellInfos,hotObj.data,hotObj.cellMetas);
		}else{
			showHandTable($dialogContent);
		}
		// if(id){
		// 	var hotObj = $('body').data(id);
		// 	showHandTable($('#cke_61_uiElement'),hotObj.mergedCellInfos,hotObj.data,hotObj.cellMetas);
		// }
	}
}


CKEDITOR.plugins.add('smartfreetable', {
	icons: 'smartfreetable',
	requires: 'widget',
	init: function(editor) {
		CKEDITOR.addCss('th, td { white-space: nowrap; }');
		var cls = 'smartfreetable-preview';
		CKEDITOR.dialog.add('smartfreetable', this.path + 'dialogs/smartfreetable.js');

		editor.on('doubleclick', function(evt) {
			var element = evt.data.element;
			var findOne = false;
			// 找到点击元素的顶点元素 
			while (true) {
				if (element.is('div') && element.hasClass(cls)) {
					findOne = true;
					break;
				} else if (element.is('body')) {
					break;
				}
				element = element.getParent();
			}

			if (findOne) {
				var id = element.getAttribute('id');
				var info = $('body').data(id);
				var mergedCellInfos = info.mergedCellInfos;
				var data = info.data;
				var cellMetas = info.cellMetas;

				showHandTable(mergedCellInfos, data, cellMetas);
				$('#freeTableDiv').data('id', id).data('element', element)
					.dialog('open');
				setTimeout('hot1.render();', 10);
			}
		});
		editor.widgets.add('smartfreetable', {
			button: '自定义表格',
			dialog: 'smartfreetable',
			draggable: false,
			requiredContent: 'div(simplebox)',
			template: '<div class="simplebox ' + cls + '"><div class="simplebox-content"></div></div>',
			init: function() {
				var element = this.element;
				var id = element.getAttribute('id');
				if (id) {
					function doSome(dbData) {
						var hotData = dbData.page.hotData;
						var hotObj = JSON.parse(hotData);
						var data = hotObj.data;
						var readTabHtml = dbData.page.readTabHtml;
						var freeTabHtml = dbData.page.freeTabHtml;
						var mergedCellInfos = hotObj.mergedCellInfos;
						$('body').data(id, hotObj);

						var insertEle = element.findOne('.simplebox-content');
						// 删除 br 标签
						insertEle.getChildren().getItem(0).remove();
						insertCkFreeTableHtml(readTabHtml, freeTabHtml, insertEle, dbData.simpleUseArgs);
					}
					ck.util.sendAjax({
						url: 'freeTable/getFreeTab',
						data: {
							pageId: pageId,
							freeTabId: id
						}
					}, doSome);
				}
				this.on('doubleclick', function(evt) {
					var element = this.element;
					var id = element.getAttribute('id');					
					SmartFreeTable.freeTableId = id;
				}, null, null, 5);
				this.on('data',function(){
					delete SmartFreeTable.freeTableId;
				})
				// else {
				// 	id = 'freeTab_' + new Date().getTime();
				// 	element.setAttribute('id', id);
				// 	showHandTable();
				// 	$('#freeTableDiv').data('id', id).data('element', element).data('forInit', true)
				// 		.dialog('open');
				// 	setTimeout('hot1.render();', 10);
				// }
			},
			upcast: function(el, data) {
				if (el.name == 'div' && el.hasClass(cls)) {
					var len = el.children[0].children.length;
					if (len == 3) {
						el.children[0].children[2].remove();
						el.children[0].children[1].remove();
					}
					return el;
				}
			}
		});
	}
});